﻿using MS.Common.Helpers;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;

namespace wpf.Triage.Behavior
{
    public delegate Point GetDragDropPosition(IInputElement theElement);
    /// <summary>
    /// ui帮助类copy  2023-06-20
    /// </summary>
    public static class UIHelpers
    {
        #region copy
        /// <summary>
        /// 检查鼠标是否位于当前行  2023-06-20
        /// </summary>
        /// <param name="theTarget"></param>
        /// <param name="pos"></param>
        /// <returns>The "Rect" Information for specific Position</returns>
        private static bool IsTheMouseOnTargetRow(Visual theTarget, GetDragDropPosition pos)
        {
            Rect posBounds = VisualTreeHelper.GetDescendantBounds(theTarget);
            Point theMousePos = pos((IInputElement)theTarget);
            return posBounds.Contains(theMousePos);
        }
        /// <summary>
        /// 获取选中Row  2023-06-20
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        private static DataGridRow GetDataGridRowItem(int index, DataGrid dgEmployee)
        {
            if (dgEmployee.ItemContainerGenerator.Status
                    != GeneratorStatus.ContainersGenerated)
                return null;

            return dgEmployee.ItemContainerGenerator.ContainerFromIndex(index)
                                                            as DataGridRow;
        }
        /// <summary>
        /// 获取当前行索引  2023-06-20
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public static int GetDataGridItemCurrentRowIndex(GetDragDropPosition pos, DataGrid dgEmployee)
        {
            int curIndex = -1;
            for (int i = 0; i < dgEmployee.Items.Count; i++)
            {
                DataGridRow itm = GetDataGridRowItem(i, dgEmployee);
                if (itm != null && IsTheMouseOnTargetRow(itm, pos))
                {
                    curIndex = i;
                    break;
                }
            }
            return curIndex;
        }
        #endregion

        #region ui
        /// <summary>
        /// 查找兄弟控件方法   2023-06-20
        /// </summary>
        public static T GetVisualBrother<T>(DependencyObject obj) where T : DependencyObject
        {
            DependencyObject parent = VisualTreeHelper.GetParent(obj);
            return GetVisualChild<T>(parent);
        }
        /// <summary>
        /// 查找子控件方法   2023-06-20
        /// </summary>
        private static T GetVisualChild<T>(DependencyObject visual) where T : DependencyObject
        {
            if (visual == null)
            {
                return null;
            }

            var count = VisualTreeHelper.GetChildrenCount(visual);
            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(visual, i);

                var childOfTypeT = child as T ?? GetVisualChild<T>(child);
                if (childOfTypeT != null)
                {
                    return childOfTypeT;
                }
            }

            return null;
        }

        /// <summary>
        /// 根据行、列索引取的对应单元格对象   2023-06-20
        /// </summary>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        public static DataGridCell GetCell(DataGrid grid, int row, int column)
        {
            if (column >= grid.Columns.Count)
            {
                return null;
            }
            DataGridRow rowContainer = GetRow(grid, row);
            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
                if (presenter == null)
                {
                    grid.ScrollIntoView(rowContainer, grid.Columns[column]);
                    presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
                }
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                if (cell == null)
                {
                    grid.ScrollIntoView(rowContainer, grid.Columns[column]);
                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                }
                return cell;
            }
            return null;
        }

        /// <summary>
        /// 获取下一个可编辑列  2023-06-20
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        public static DataGridCell GetNextCell(this DataGrid grid, int row, int col)
        {
            try
            {
                if (col + 1 < grid.Columns.Count)
                {
                    var cell = GetCell(grid, row, col + 1);
                    if (cell == null)
                    {
                        return GetNextCell(grid, row, col + 1);
                    }
                    else if (cell.IsReadOnly)
                    {
                        return GetNextCell(grid, row, col + 1);
                    }
                    else
                    {
                        return cell;
                    }
                }
                else
                {
                    return GetNextCell(grid, row + 1, 0);
                }
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error(ex);
                return null;
            }
        }
        /// <summary>
        /// 获取上一个可编辑列  2023-06-20
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        public static DataGridCell GetPreCell(this DataGrid grid, int row, int col)
        {
            try
            {
                if (row < 0)
                {
                    return null;
                }
                if (col - 1 >= 0)
                {
                    var cell = GetCell(grid, row, col - 1);
                    if (cell == null)
                    {
                        return GetPreCell(grid, row, col - 1);
                    }
                    else if (cell.IsReadOnly)
                    {
                        return GetPreCell(grid, row, col - 1);
                    }
                    else
                    {
                        return cell;
                    }
                }
                else
                {
                    return GetPreCell(grid, row - 1, grid.Columns.Count);
                }
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error(ex);
                return null;
            }
        }

        /// <summary>
        /// 根据行索引取的行对象    2023-04-24
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static DataGridRow GetRow(DataGrid grid, int index)
        {
            DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
            if (row == null)
            {
                grid.ScrollIntoView(grid.Items[index]);
                row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
            }
            return row;
        }

        /// <summary>
        /// 获取指定类型的子元素    2023-06-20
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual visual = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = visual as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(visual);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }
        #endregion
    }
}
